package zj.it.bhne.huanxin.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.widget.EaseConversationList;
import com.hyphenate.easeui.widget.EaseTitleBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import zj.it.bhne.huanxin.R;

public class SessionFragment extends Fragment {

    private EaseConversationList session_list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_session, container, false);
        initView(view);
        //透明状态栏
        Objects.requireNonNull(getActivity()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        View views = new View(getContext());
        ViewGroup.LayoutParams params = new ViewGroup
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
        views.setLayoutParams(params);
        views.setBackgroundColor(Color.rgb(18,150,219));

        ViewGroup decorView = (ViewGroup) Objects.requireNonNull(getActivity()).getWindow().getDecorView();
        decorView.addView(views);
        return view;
    }

    private Handler handler = new Handler();

    private void initView(View view) {
        EaseTitleBar title_bar = view.findViewById(R.id.title_bar);
        title_bar.setTitle("会话");
        session_list = view.findViewById(R.id.session_list);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
                    final List<EMConversation> emConversations = new ArrayList<>();
                    for (int i = 0; i < conversations.size(); i++) {
                        emConversations.add(conversations.get(i));
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //初始化，参数为会话列表集合
                            session_list.init(emConversations);
                            //刷新列表
                            session_list.refresh();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();



    }
}
